﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Jayden.Dll.Core;
using Jayden.Dll.Core.Persistence;
using Jayden.Dll.Cards.Poker;

namespace Jayden.Windows.PokerWatcher
{
	public partial class PokerWatcherWindowForm : Form
	{
		public Configuration Configuration { get; set; }
		public UnitOfWork UnitOfWork { get; set; }

		public PokerWatcherWindowForm(Configuration config, UnitOfWork uow)
		{
			Configuration = config;
			UnitOfWork = uow;
			InitializeComponent();
			try
			{
				lvPlayers.BeginUpdate();
				lvPlayers.Items.Clear();
				TexasHoldemPlayerHandSummaryDataAccess playerHandDataAccess = UnitOfWork.DataAccessManager.GetDataAccess<TexasHoldemPlayerHandSummary>() as TexasHoldemPlayerHandSummaryDataAccess;
				List<TexasHoldemPlayer> players = playerHandDataAccess.ReadPlayersLike("%");
				foreach (TexasHoldemPlayer player in players)
				{
					string[] values = new string[]
				{
					player.ProviderName,
					player.PlayerName,
					player.HandCount.ToString(),
					player.CardsCount.ToString() + " (" + (player.CardsCount * 100.0 / player.HandCount).ToString("0.00") + " %)"
				};
					ListViewItem item = new ListViewItem(values);
					lvPlayers.Items.Add(item);
				}
			}
			catch (Exception e)
			{

			}
			finally
			{
				lvPlayers.EndUpdate();
			}
		}

		private void RefreshPlayerHands()
		{
			lvPlayerHands.BeginUpdate();
			lvPlayerHands.Items.Clear();
			bool withCardsOnly = cbHands.Checked;
			bool withShowDownOnly = cbShowDown.Checked;
			if (lvPlayers.SelectedItems.Count != 0)
			{
				TexasHoldemPlayerHandSummaryDataAccess playerHandDataAccess = UnitOfWork.DataAccessManager.GetDataAccess<TexasHoldemPlayerHandSummary>() as TexasHoldemPlayerHandSummaryDataAccess;
				List<TexasHoldemPlayerHandSummary> hands = playerHandDataAccess.Read(lvPlayers.SelectedItems[0].SubItems[1].Text);
				hands.Sort(delegate(TexasHoldemPlayerHandSummary a, TexasHoldemPlayerHandSummary b)
				{
					return b.HandSummary.HandDateTime.CompareTo(a.HandSummary.HandDateTime);
				}); 
				foreach (TexasHoldemPlayerHandSummary hand in hands)
				{
					if (withCardsOnly && (hand.PlayerCards == null || hand.PlayerCards.Count != 2))
						continue;
					if (withShowDownOnly && !(hand.FoldStreet == hand.HandSummary.LastStreet && hand.HandSummary.LastStreet == Dll.Cards.Poker.Enums.PokerStreetType.Showdown))
						continue;
					string[] values = new string[]
					{
						hand.HandSummary.HandDateTime.ToString("yyyy-MM-dd HH-mm-ss"),
						hand.PlayerHand,
						hand.HandSummary.Community,
						hand.PlayerFinalHand == null ? string.Empty : hand.PlayerFinalHand.Description,
						(hand.PlayerBalance / 100.0).ToString("0.00") + " " + hand.HandSummary.Currency,
						(hand.FoldStreet == hand.HandSummary.LastStreet && hand.HandSummary.HasShowdown).ToString().ToLowerInvariant(),
						hand.PositionType
					};
					ListViewItem item = new ListViewItem(values);
					item.UseItemStyleForSubItems = false;
					if (hand.PlayerBalance != 0)
						item.SubItems[4].ForeColor = hand.PlayerBalance > 0 ? Color.Green : Color.Red;
					if (hand.FoldStreet == hand.HandSummary.LastStreet && hand.HandSummary.HasShowdown)
					{
						item.SubItems[5].ForeColor = Color.Green;
						for (int c = 0; c < item.SubItems.Count;c++)
							item.SubItems[c].BackColor = Color.LightGreen;
					}
					item.Tag = hand;
					lvPlayerHands.Items.Add(item);
				}
			}
			lvPlayerHands.EndUpdate();
		}

		private void lvPlayers_SelectedIndexChanged(object sender, EventArgs e)
		{
			RefreshPlayerHands();
		}

		private void cbHands_CheckedChanged(object sender, EventArgs e)
		{
			RefreshPlayerHands();
		}

		private void cbShowDown_CheckedChanged(object sender, EventArgs e)
		{
			RefreshPlayerHands();
		}

		private void lvPlayerHands_DoubleClick(object sender, EventArgs e)
		{
			if (lvPlayerHands.SelectedItems != null && lvPlayerHands.SelectedItems.Count > 0)
			{
				TexasHoldemPlayerHandSummary playerHand = lvPlayerHands.SelectedItems[0].Tag as TexasHoldemPlayerHandSummary;
				PokerWatcherTableViewerForm viewer = new PokerWatcherTableViewerForm(Configuration, UnitOfWork, playerHand.HandSummary);
				viewer.Show();
			}
		}
	}
}
